package com.xlf.service.judge.strategy.judgeMachine.content;

import com.xlf.pojo.base.Judge;
import com.xlf.pojo.base.Problem;
import com.xlf.pojo.dto.RunResult;
import com.xlf.pojo.dto.TestJudgeReq;
import com.xlf.service.judge.exception.CompileError;
import com.xlf.service.judge.io.File;
import com.xlf.service.judge.io.LanguageConfigLoader;
import com.xlf.service.judge.io.ResourceLoader;
import com.xlf.service.judge.pojo.*;
import com.xlf.service.judge.strategy.judgeMachine.AbstractJudgeMachineStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.function.Consumer;
import java.util.function.Function;

/**
 * @Author xlf 2734029211@qq.com
 * @Date 2023/9/18 16:56
 * @Description: 策判题机策略上下文，只做编译和运行，不做判题。
 *                通过该上下文你可以得到语言配置加载器、测试用例加载器。
 * @Version 1.0
 */
@Slf4j
@Component
public class JudgeMachineContent {


    @Autowired
    private AbstractJudgeMachineStrategy abstractJudgeMachineStrategy;

    /**
     * 调用具体的判题机运行该题目，得到结果，不做判题。
     * @param problem 题目信息
     * @param judge 判题信息
     * @param consumer 也行你需要在编译后做一些操作
     * @throws CompileError 编译错误
     */
    public JudgeResult execute(Problem problem, Judge judge, Consumer<Judge> consumer) throws CompileError {
        return abstractJudgeMachineStrategy.judge(problem,judge,consumer);
    }

    /**
     * 编译
     * @param language 语言名称，见配置文件
     * @param code 提交的代码
     * @return 编译后的文件
     * @throws CompileError 编译异常
     */
    public Object compile(String language,String code) throws CompileError{
        return abstractJudgeMachineStrategy.doCompile(language,code);
    }

    /**
     * 运行
     * @param language 语言名称，见配置文件
     * @param file 编译后的源代码，和Compile有关
     * @param testCaseInput 测试用例输入
     * @param runLimit 沙箱限制
     * @return
     */
    public RunResult doRun(String language, Object file, File testCaseInput, RunLimit runLimit){
        return abstractJudgeMachineStrategy.doRun(language,file,testCaseInput,runLimit);
    }

    /**
     *
     * @param testJudgeReq
     * @return
     * @throws CompileError
     */
    public RunResult testJudge(TestJudgeReq testJudgeReq) throws CompileError{
        return abstractJudgeMachineStrategy.TestJudge(testJudgeReq);
    }


    public LanguageConfigLoader getLanguageConfigLoader() {
        return abstractJudgeMachineStrategy.getLanguageConfigLoader();
    }

    public ResourceLoader getResourceLoader() {
        return abstractJudgeMachineStrategy.getResourceLoader();
    }


}
